
$(function(){
	top.$('#player_modal').on('hidden', function () {
		var audio = top.document.getElementById("audio");
		 if(!audio.paused){                 
             audio.pause();// 暂停播放  
         }  
	});
});
//设置播放参数
var palyer = function (audioPath) {
	top.$("audio").attr("src",audioPath);
	addPlayerList(audioPath);
 	showPlayerList();
 	top.$("#player_modal").modal("show");
 	playCotrol();
};

/*
 * 添加播放的录音到播放列表
 */
var addPlayerList = function (audioPath){
	var mideaType = audioPath.substr(audioPath.lastIndexOf('.'));
 	var mideaName = audioPath.substr(audioPath.lastIndexOf('/')+1);
 	var newMideaName = mideaName.substr(0,mideaName.indexOf('C',1)+15);
 	var reg = /[A-Za-z]+/g;
 	mideaName = newMideaName.replace(reg,'-');
 	var midea = {};
 	midea.path = audioPath;
 	midea.name = mideaName+mideaType;
 	if(!top.$("body").data("mideas")){
 		var mideas = new Array();
 		mideas.push(midea);
 		top.$("body").data("mideas",mideas);
 	} else {
 		var mideas = top.$("body").data("mideas");
 		var temp = new Array();
 		//copy数组，解决IE下跨页面无法对原来数组操作的问题
 		for (var i = 0; i < mideas.length; i++) {
 			temp[i] = mideas[i];
		}
 		temp.unshift(midea);//把新数据放到第一个
 		top.$("body").data("mideas",unique(temp));
 	}
};

/*
 * 数组去重
 */
var unique = function (arr) {
	var tmp = new Array();
	for (var i = 0; i < arr.length; i++) {
		tmp.push(arr[i].name);
	}
	var result = [], hash = {};
    for (var i = 0, elem; (elem = tmp[i]) != null; i++) {
        if (!hash[elem]) {
            result.push(arr[i]);
            hash[elem] = true;
        }
    }
    return result;
};

/*
 * 展示播放列表
 */
var showPlayerList = function(){
	playerListHtml();
	top.$(".player-list a:first").addClass("text-error");
	top.$(".player-list a").click(function(){
		top.$("audio").attr("src",this.href);
		top.$(".text-error").removeClass();
		top.$(this).addClass("text-error");
		return false;
	});
	top.$(".player-list .icon-remove").click(function(){
		//如果删除的是正在播放的录音，则切换到下一个，如果没有下一个，则停止播放
		if(top.$(this).parents("tr").find(".text-error").length){
			var aTags = $(this).parents("tr").next().find("a");
			if (aTags.length){
				top.$(aTags[0]).click();
			} else {
				top.$("audio").attr("src","");
			}
		}
		top.$(this).parents("tr").remove();
		removePlayerList($(this).data("ix"));
	});
};

/*
 * 绘制播放列表
 */
var playerListHtml = function playerListHtml(index){
	var mideaHtml = "<table class='table'><tbody>";
	var mideas = parent.$("body").data("mideas");
	for (var i = 0; i < mideas.length; i++) {
		mideaHtml += "<tr><td>"+ (i+1) +"</td><td><a href='"
		+ mideas[i].path + "'>"+ mideas[i].name 
		+ "</a></td><td><i class='icon-remove' data-ix='"+i
		+ "' style='cursor:pointer;'></i></td></tr>";
	}
	mideaHtml += "</tbody></table>";
	top.$(".player-list").empty();
	top.$(".player-list").append(mideaHtml);
};

/*
 * 删除播放列表中的单个录音
 */
var removePlayerList = function (index){
	top.$("body").data("mideas").splice(index,1);//移除删除的数据
	var trTags = top.$(".player-list table tr");
	for (index ; index < trTags.length; index++) {
		top.$(trTags[index]).find("td:first")[0].innerHTML= index+1;
		top.$(trTags[index]).find("td:last .icon-remove").attr("data-ix",index);
	}
};

/*
 * 播放完成后切换到下一个
 */
var playCotrol = function () {
	var audio = top.document.getElementById("audio");
	audio.addEventListener("timeupdate", function() {
		if (audio.currentTime == audio.duration) {
			var nextMidea = top.$(".text-error").parents("tr").next().find("a");
			if(nextMidea.length) {
				top.$(nextMidea[0]).click();
				return;
			}
		}
    }, false);
};